package clock._11.borovskih;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.InputStream;
import java.util.GregorianCalendar;

public class DrawClock extends JPanel {
    private int sec;
    private int min;
    private int hour;

    public void drawOne(Graphics g) {
        Graphics2D g2 = (Graphics2D) g;
        g2.setColor(Color.BLACK);
        g2.setStroke(new BasicStroke(4));
        g2.drawLine(-10, 0, 10, 0);
    }

    public void drawTwo(Graphics g) {
        Graphics2D g2 = (Graphics2D) g;
        g2.setColor(Color.BLACK);
        g2.setStroke(new BasicStroke(4));
        g2.drawLine(-10, -3, 10, -3);
        g2.drawLine(-11, 2, 11, 2);
    }

    public void drawThree(Graphics g) {
        Graphics2D g2 = (Graphics2D) g;
        g2.setColor(Color.BLACK);
        g2.setStroke(new BasicStroke(4));
        g2.drawLine(-10, -5, 10, -5);
        g2.drawLine(-7, 0, 7, 0);
        g2.drawLine(-11, 5, 11, 5);
    }

    public void drawFour(Graphics g) {
        Graphics2D g2 = (Graphics2D) g;
        g2.setColor(Color.BLACK);
        g2.setStroke(new BasicStroke(4));
        g2.drawLine(-10, -11, 10, -11);
        g2.drawLine(-10, -11, -10, 11);
        g2.drawLine(10, -11, 10, 11);
        g2.drawLine(-10, 10, 10, 10);

        g2.setStroke(new BasicStroke(3));
        g2.drawLine(-2, -11, -2, 0);
        g2.drawLine(-2, 0, -7, 3);

        g2.drawLine(4, -11, 4, 3);
        g2.drawLine(4, 3, 9, 3);
    }

    public void drawFive(Graphics g) {
        Graphics2D g2 = (Graphics2D) g;
        g2.setColor(Color.BLACK);
        g2.setStroke(new BasicStroke(4));
        g2.drawLine(-10, -11, 8, -11);
        g2.drawLine(-6, -13, -6, 7);
        g2.drawLine(-10, 0, 4, 0);
        g2.drawLine(4, 0, 4, 11);
        g2.drawLine(-11, 11, 10, 11);
    }

    public void drawSix(Graphics g) {
        Graphics2D g2 = (Graphics2D) g;
        g2.setColor(Color.BLACK);
        g2.setStroke(new BasicStroke(4));
        g2.drawLine(-10, -5, 10, -5);       //горизонт
        g2.drawLine(0, -8, 0, -5);       //верт

        g2.drawLine(-2, 0, -10, 9);
        g2.drawLine(2, 0, 10, 9);
    }

    public void drawSeven(Graphics g) {
        Graphics2D g2 = (Graphics2D) g;
        g2.setColor(Color.BLACK);
        g2.setStroke(new BasicStroke(4));
        g2.drawLine(-10, -5, 7, -9); //верт
        g2.drawLine(-7, -11, -7, 11);
        g2.drawLine(-7, 11, 8, 11);
        g2.drawLine(8, 11, 8, 8);
    }

    public void drawEight(Graphics g) {
        Graphics2D g2 = (Graphics2D) g;
        g2.setColor(Color.BLACK);
        g2.setStroke(new BasicStroke(4));
        g2.drawLine(-3, -8, -13, 8);
        g2.drawLine(3, -8, 13, 8);
    }

    public void drawNine(Graphics g) {
        Graphics2D g2 = (Graphics2D) g;
        g2.setColor(Color.BLACK);
        g2.setStroke(new BasicStroke(4));
        g2.drawLine(-6, -10, -6, 3);
        g2.drawLine(-6, 3, -10, 10);

        g2.drawLine(-10, -6, 3, -6);
        g2.drawLine(3, -6, 3, 10);
        g2.drawLine(3, 10, 10, 10);
        g2.drawLine(10, 10, 10, 8);
    }

    public void drawTen(Graphics g) {
        Graphics2D g2 = (Graphics2D) g;
        g2.setColor(Color.BLACK);
        g2.setStroke(new BasicStroke(4));
        g2.drawLine(-10, 0, 10, 0);
        g2.drawLine(0, -11, 0, 11);
    }

    public void drawEleven(Graphics g) {
        Graphics2D g2 = (Graphics2D) g;
        g2.translate(-10, 0);
        g2.setColor(Color.BLACK);
        g2.setStroke(new BasicStroke(4));
        drawTen(g2);
        g2.drawLine(16, 0, 36, 0);
    }

    public void drawTwelve(Graphics g) {
        Graphics2D g2 = (Graphics2D) g;
        g2.translate(-10, -5);
        g2.setColor(Color.BLACK);
        g2.setStroke(new BasicStroke(4));
        drawTen(g2);
        g2.drawLine(18, -3, 34, -3);
        g2.drawLine(16, 3, 36, 3);
    }

    public void drawHArrow(Graphics g) {
        final Graphics2D g2 = (Graphics2D) g;
        g2.setColor(new Color(0xe23f69));
        g2.setStroke(new BasicStroke(5));
        g2.drawLine(0, 0, 0, -60);
    }

    public void drawMArrow(Graphics g) {
        final Graphics2D g2 = (Graphics2D) g;
        g2.setColor(new Color(0xd61346));
        g2.setStroke(new BasicStroke(4));
        g2.drawLine(0, 0, 0, -90);
    }

    public void drawSArrow(Graphics g) {
        final Graphics2D g2 = (Graphics2D) g;
        g2.setColor(new Color(0xa2062e));
        g2.setStroke(new BasicStroke(3));
        g2.drawLine(0, 0, 0, -105);
    }

    public void clear(Graphics g) {
        final Graphics2D g2 = (Graphics2D) g;
        g2.setColor(new Color(0xA9A9A9));
        g2.fillRect(0, 0, 800, 640);
    }

    public DrawClock() {
        Timer t = new Timer(1000, new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                final GregorianCalendar cc = new GregorianCalendar();
                sec = cc.get(GregorianCalendar.SECOND);
                min = cc.get(GregorianCalendar.MINUTE);
                hour = cc.get(GregorianCalendar.HOUR);
                repaint();
            }
        });

        t.start();
    }

    protected void paintComponent(Graphics g) {

        Graphics2D gg = (Graphics2D) g;
        gg.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        clear(gg);
        InputStream im = DrawClock.class.getResourceAsStream("red_backgrounds.jpg");
        Image image = null;
        try {
            image = ImageIO.read(im);

        } catch (IOException e) {
            e.printStackTrace();
        }
        gg.drawImage(image, 0, 0, null);



        g.translate(315, 205);
        final int R = 135;
        final int Rin = 125;
        final int Rinin = 115;

        gg.setColor(new Color(0x790d12));
        gg.fillOval(-R, -R, 2 * R, 2 * R);

        gg.setColor(new Color(0xe16c72));
        gg.fillOval(-Rin, -Rin, 2 * Rin, 2 * Rin);

        gg.setColor(Color.LIGHT_GRAY);
        gg.fillOval(-Rinin, -Rinin, 2 * Rinin, 2 * Rinin);
        drawHours(gg);

        Graphics2D gs = (Graphics2D) gg.create();
        gs.rotate(sec * Math.PI / 30);
        drawSArrow(gs);
        gs.dispose();

        Graphics2D gm = (Graphics2D) gg.create();
        gm.rotate(min * Math.PI / 30);
        drawMArrow(gm);
        gm.dispose();

        Graphics2D gh = (Graphics2D) gg.create();
        gh.rotate(hour * Math.PI / 6);
        drawHArrow(gh);
        gh.dispose();

        gg.setColor(new Color(0xe06c6f));
        gg.fillOval(-7, -7, 14, 14);


    }

    private void drawHours(Graphics2D g2d) {
        for (int h = 1; h <= 12; h++) {
            Graphics2D g = (Graphics2D) g2d.create();
            g.rotate(h * Math.PI / 6);
            g.translate(0, -90);
            g.rotate(-h * Math.PI / 6);
            switch (h) {
                case 1:
                    drawOne(g);
                    break;
                case 2:
                    drawTwo(g);
                    break;
                case 3:
                    drawThree(g);
                    break;
                case 4:
                    drawFour(g);
                    break;
                case 5:
                    drawFive(g);
                    break;
                case 6:
                    drawSix(g);
                    break;
                case 7:
                    drawSeven(g);
                    break;
                case 8:
                    drawEight(g);
                    break;
                case 9:
                    drawNine(g);
                    break;
                case 10:
                    drawTen(g);
                    break;
                case 11:
                    drawEleven(g);
                    break;
                case 12:
                    drawTwelve(g);
                    break;
            }
            g.dispose();
        }
    }
}
